package com.phb.puhuibao.web.controller;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.idap.clinic.entity.UploadFile;
import com.idap.web.common.controller.Commons;
import com.idp.pub.service.IBaseService;
import com.idp.pub.utils.DESUtils;
import com.idp.pub.utils.RSAUtils;
import com.idp.pub.web.controller.BaseController;
import com.opensymphony.oscache.util.StringUtil;
import com.phb.puhuibao.common.Functions;
import com.phb.puhuibao.common.InitListener;
import com.phb.puhuibao.entity.Company;
import com.phb.puhuibao.entity.ExperienceValue;
import com.phb.puhuibao.entity.Invite;
import com.phb.puhuibao.entity.MobileUser;
import com.phb.puhuibao.entity.MobileUserExtra;
import com.phb.puhuibao.entity.MobileUserHexagram;
import com.phb.puhuibao.entity.MuserFollow;
import com.phb.puhuibao.entity.UserCard;
import com.phb.puhuibao.entity.UserSession;
import com.phb.puhuibao.service.MobileUserService;
import com.phb.puhuibao.service.impl.MuserFollowServiceImpl;

@Controller
@RequestMapping(value = "/company")
public class CompanyController extends BaseController<Company, String> {
	final Log log = LogFactory.getLog(CompanyController.class);
	@Resource(name = "companyService")
	public void setBaseService(IBaseService<Company, String> baseService) {
		super.setBaseService(baseService);
	}
	@Resource(name = "uploadFileService")
	private IBaseService<UploadFile, String> uploadFileService;
	@Resource(name = "mobileUserService")
	private IBaseService<MobileUser, String> mobileUserService;
	@Resource(name = "commons")
	private Commons commons;
	//-----获取企业信息-----
	@RequestMapping(value="getCompanyById")
	@ResponseBody
	public Map<String, Object> getCompanyById(@RequestParam String userId){
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String,Object> params = new HashMap<String,Object>();						
		params.put("userId", userId);
		
		try {
			Company entity = this.getBaseService().unique(params);	
		   if(entity==null){
			   data.put("result", entity);
				data.put("message", "没有该企业！");
				data.put("status", -1);  
		   }else{
			    data.put("result", entity);
				data.put("message", "获取企业信息成功！");
				data.put("status", 1); 
		   }
			
		} catch (Exception e) {
			data.put("message", "获取企业信息失败！");
			data.put("status", 0);			
		}
	return data;
	}


	//-----编辑企业信息-----
		@RequestMapping(value="modifyCompanyInformation")
		@ResponseBody
		public Map<String, Object> modifyCompanyInformation(   @RequestParam String companyId,           @RequestParam String companyName,   
													           @RequestParam String companyEmail,        @RequestParam String companyAddress,
													           @RequestParam String companyProfession,   @RequestParam String companyCharacter,
													           @RequestParam String companyScale,        @RequestParam String companyDetails,
													           @RequestParam String id ,                 @RequestParam String userId          ){
			Map<String, Object> data = new HashMap<String, Object>();
			Map<String,Object> params = new HashMap<String,Object>();
			
			try {
				
					params.put("userId", userId);							
			Company entity = this.getBaseService().unique(params);	
			if(entity==null){
			Company company = new Company();
			company.setUserId(userId);
			company.setBusinessLicense(id);	
			company.setCompanyName(companyName);
			company.setCompanyAddress(companyAddress);
			company.setCompanyEmail(companyEmail);
			company.setCompanyProfession(companyProfession);
			company.setCompanyCharacter(companyCharacter);
			company.setCompanyScale(companyScale);
			company.setCompanyDetails(companyDetails);
			this.getBaseService().save(company);
			data.put("result", company);
			}else{
				if (entity.getCompanyLogo()!= null && !"".equals(entity.getCompanyLogo())) {
					params = new HashMap<String,Object>();
					params.put("id", entity.getCompanyLogo());
					uploadFileService.delete(params);
				}
				entity.setCompanyLogo(id);	
				entity.setCompanyName(companyName);
				entity.setCompanyAddress(companyAddress);
				entity.setCompanyEmail(companyEmail);
				entity.setCompanyProfession(companyProfession);
				entity.setCompanyCharacter(companyCharacter);
				entity.setCompanyScale(companyScale);
				entity.setCompanyDetails(companyDetails);
				this.getBaseService().save(entity);
			    this.getBaseService().update(entity);
			    data.put("result", entity);
			}
			    
				data.put("message", "保存成功！");
				data.put("status", 1);
			} catch (Exception e) {
				data.put("message", "网络异常！");
				data.put("status", 0);			
			}
		return data;
		}
	
		//-----展示给用户看的企业信息-----
		@RequestMapping(value="showCompanyForUser")
		@ResponseBody
		public Map<String, Object> showCompanyForUser(@RequestParam String companyId){
			Map<String, Object> data = new HashMap<String, Object>();
			Map<String,Object> params = new HashMap<String,Object>();						
			params.put("companyId", companyId);
			
			try {
				Company entity = this.getBaseService().unique(params);	
			   if(entity==null){
					data.put("message", "没有该企业！");
					data.put("status", -1);  
			   }else{
				   params = new HashMap<String,Object>();
				   params.put("userId", entity.getUserId());
				  MobileUser user = mobileUserService.unique(params);
				  if(user.getCertifyStatus()==2||user.getCertifyStatus()==3){
					  data.put("isCertify", 1);
				  }else{
					  data.put("isCertify", 0); 
				  }
				  entity.setRegistrationNum("");
				  entity.setCompanyEmail("");
				  entity.setBusinessLicense("");
				  entity.setImportant("");
			    data.put("result", entity);
				data.put("message", "展示企业信息成功！");
				data.put("status", 1); 
			   }
				
			} catch (Exception e) {
				data.put("message", "展示企业信息失败！");
				data.put("status", 0);			
			}
		return data;
		}
	
}